package org.sikyon.chocolatebiscuit.model;

import org.junit.Test;
import org.sikyon.chocolatebiscuit.model.exceptions.MoneyFormatException;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.fail;


public class MoneySpec {

    @Test
    public void parsingAFloatingPointNumberWithMoreThanTwoDecimalsAlwaysRoundsTheDecimalsDown() {
        assertThat(Money.parse("86.151").toString(), is("$86.15"));
        assertThat(Money.parse("86.159").toString(), is("$86.15"));
    }

    @Test
    public void aStringCanBePrefixedByTheCurrencySign() {
        assertThat(Money.parse("$86.15").toString(), is("$86.15"));
    }

    @Test
    public void unparseableStringsThrowAMoneyFormatException() {
        try {
            Money.parse("hello");
            fail("'hello' is unparseable and should have thrown an exception");
        } catch (MoneyFormatException e) {
            // as you can see, the regex is part of the spec
            assertThat(e.getMessage(), is("'hello' couldn't be parsed into a Money. Only Strings abiding to the \\$?[0-9]+\\.[0-9]* regular expression are parseable."));
        }
    }
}

